Page({
    data: {
      height: 0,
      params: {
        page: 1,
        size: 9999999,
        userId: wx.getStorageSync('userId')
      },
      list: [],
      addCartype: false //购物车状态
    },

    onLoad () {
      this.getHeight()
    },

  //获取屏幕高度
    getHeight() {
      var _this = this
      wx.getSystemInfo({
        success: function (res) {
          let clientHetght = res.windowHeight
          let clientWidth = res.windowWidth
          let ratio = 750 / clientWidth
          let height = clientHetght * ratio - 102
          _this.setData({
            height: height
          })
        },
      })
    },

    //跳转漂流日记

    piaoliuNot () {
      wx.navigateTo({
        url: '/package/my/pages/piaoliuNot/piaoliuNot',
      })
    },


    //点击发布动态
    fabuDongtai () {
      wx.navigateTo({
        url: '/package/my/pages/fabuDongtai/fabuDongtai',
      })
    },

    getList (params) {
      var _this = this

      wx.showLoading({
        title: '加载中',
        mask: true
      })
      wx.request({
        url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxTrends/findMyTrends/auth',
        data: params,
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
          "token": wx.getStorageSync('token'),
          "uid": wx.getStorageSync('uuid')
        },
        success: (res) => {
          if (res.data.code == 200) {

            wx.hideLoading()
            for (var i = 0;i< res.data.data.length; i++) {
              if (res.data.data[i].bookInfos == '' || res.data.data[i].bookInfos == null || res.data.data[i].bookInfos == undefined) {
                res.data.data[i].pictures = res.data.data[i].pictures.split(',')
              }
            }
            _this.setData({
              list: res.data.data
            })
            // var list = res.data.data
            // _this.setData({
            //   list: list
            // })
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    },


    deleteStatus (e) {

      var _this = this
      var trendsid = e.currentTarget.dataset.trendsid
      var params = {
        ids: trendsid
      }
      wx.request({
        url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxTrends/del/auth',
        data: params,
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
          "token": wx.getStorageSync('token'),
          "uid": wx.getStorageSync('uuid')
        },
        success: (res) => {
          if (res.data.code == 200) {
            wx.showToast({
              title: '删除成功',
              icon: 'none'
            })

            for (var i = 0;i< _this.data.list.length;i++) {
              if (trendsid == _this.data.list[i].trendsId) {
                var list = _this.data.list
                list.splice(i,1)
                _this.setData({
                  list: list
                })
              }
            }
          }else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    },




  onShow () {
      this.getList(this.data.params)
  },
    //刷新好友
  findPerson () {
    this.getList(this.data.params)
  },

  openDetail (e) {
    console.log(e)
    // openDetail
    // return
    var bookid = e.currentTarget.dataset.bookid
    wx.navigateTo({
      url: '/package/index/pages/bookdetail/bookdetail?bookId=' + bookid,
    })
  },

  //加入购物车

  addCar(e) {
    var carid = e.currentTarget.dataset.carid

    this.addCarApi(carid)
  },

  //添加购物车

  addCarApi(id) {

    var _this = this

    var params = {
      userId: wx.getStorageSync('userId'),
      bookInfoId: id
    }

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxShoppingTrolleys/mod/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        console.log(res)

        if (res.data.code == 200) {
          wx.showToast({
            title: '购物车添加成功',
            icon: 'none'
          })
          _this.setData({
            addCartype: true
          })
          setTimeout(function () {
            _this.setData({
              addCartype: false
            })
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  goDetail(e) {
    var bookid = e.currentTarget.dataset.bookinfoid

    // var bookId = e.currentTarget.dataset.bookid
    wx.navigateTo({
      url: '/package/index/pages/bookdetail/bookdetail?bookId=' + bookid,
    })
  }
})
